using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using TVT.DataAccess.Repositories;
using System.Transactions;
using TVT.Common.Exceptions;

namespace TVT.BizLayer.Entities
{
    [Serializable]
    public class Account: BaseEntity<Account>
    {
        #region attribute

        private static DBRepository repository = DBRepository.GetInstance();

        private int id;
        private string name;
        private decimal interest;
        private int idCompany;
        private Company company;
        private int idClearing;

        #endregion

        #region properties


        public int IdCompany
        {
            get { return idCompany; }
            set { idCompany = value; }
        }

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public decimal Interest
        {
            get { return interest; }
            set { interest = value; }
        }

        public Company Company
        {
            get
            {
                if (company == null)
                    company = TVT.BizLayer.Entities.Company.Find(IdCompany);
                return company;
            }
            set { company = value; }
        }

        public int IdClearing
        {
            get { return idClearing; }
            set { idClearing = value; }
        }

        #endregion        

        #region Constructors

        public Account(){}


        public Account(int id)
        {
            this.id = id;
        }

        #endregion     
    
        #region Methods

        public override void Insert()
        {
            throw new NotImplementedException();
        }

        public override void Update()
        {
            throw new NotImplementedException();
        }

        public override void Delete()
        {
           Delete(this.id);
        }

        public override void Save()
        {
            using (TransactionScope tran = new TransactionScope())
            {
                try
                {
                    this.id = repository.SaveAccount(this.id, this.name, this.interest, this.idCompany, this.idClearing);
                }
                catch (Exception)
                {
                    tran.Dispose();
                    throw;
                }
                tran.Complete();
            }
        }

        public static void Delete(int id)
        {
            using (TransactionScope tran = new TransactionScope())
            {
                try
                {
                    repository.DeleteAccount(id);
                }
                catch (Exception)
                {
                    tran.Dispose();
                    throw;
                }
                tran.Complete();
            }
        }
        public static void DeleteByProduct(int idProduct)
        {
            repository.DeleteAccountByProduct(idProduct);
        }

        public static List<Account> FindByIdProduct(int idProduct)
        {
            return MapToEntities(repository.FindAccountByIdProduct(idProduct));
        }

        public static List<Account> FindAll()
        {
            return MapToEntities(repository.FindAllAccounts());
        }

        public static List<Account> FindByUser(Guid userId)
        {
            return MapToEntities(repository.FindAccountsByUser(userId));
        }


        public static List<Account> FindByUserAndIdClearings(Guid userId,List<int> idsClearing)
        {
            return MapToEntities(repository.FindAccountsByUserAndIdClearings(userId,idsClearing));
        }


        public static Account FindByName(string name)
        {
            return MapToEntity(repository.FindAccountIdByName(name));
        }

        public static Account Find(int idAccount)
        {
            return MapToEntity(repository.FindAccount(idAccount));
        }

        #endregion


        public static List<Account> FindAllByCompanies(List<int> idCompanies)
        {
            return MapToEntities(repository.FindAllAccountsByCompanies(idCompanies));
        }
    }
}